﻿package com.mui.manager
{
    import flash.accessibility.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class ToolTipsManager extends Sprite
    {
        private var area:DisplayObject;
        private var toolTipContent:IToolTip;
        private var toolTipLayer:DisplayObjectContainer;
        private var delayTimer:Timer;
        private var _this:ToolTipsManager;
        private static var toolClassMap:Object = {};
        private static var classId:int = 0;
        private static var _instance:ToolTipsManager = null;
        private static var _defaultRenderClass:Class;

        public function ToolTipsManager(param1:DisplayObjectContainer)
        {
            visible = false;
            var _loc_2:Boolean = false;
            mouseChildren = false;
            mouseEnabled = _loc_2;
            this.toolTipLayer = param1;
            this._this = this;
            param1.addChild(this);
            return;
        }// end function

        public function targetMouseHandler(event:MouseEvent) : void
        {
            var _loc_2:DisplayObject = null;
            var _loc_3:int = 0;
            switch(event.type)
            {
                case MouseEvent.MOUSE_OUT:
                case MouseEvent.MOUSE_DOWN:
                case MouseEvent.MOUSE_UP:
                {
                    _instance.hide();
                    break;
                }
                case MouseEvent.MOUSE_MOVE:
                {
                    _instance.move(new Point(event.stageX, event.stageY));
                    break;
                }
                case MouseEvent.MOUSE_OVER:
                {
                    _loc_2 = event.currentTarget as DisplayObject;
                    if (_instance.area != _loc_2)
                    {
                        _instance.hide();
                    }
                    if (_loc_2.alpha == 1)
                    {
                        _instance.area = event.currentTarget as DisplayObject;
                        _loc_3 = 100;
                        if (_loc_2 is IToolTipItem)
                        {
                            _loc_3 = (_loc_2 as IToolTipItem).reactionTime;
                        }
                        this.startDelayCount(_loc_3);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function show(param1:DisplayObject) : void
        {
            var _loc_2:Object = null;
            var _loc_3:Object = null;
            var _loc_4:DisplayObjectContainer = null;
            this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
            this.area.addEventListener(Event.REMOVED_FROM_STAGE, this.removedHandler);
            if (param1.accessibilityProperties)
            {
                this.clearConetnt();
                _loc_2 = toolClassMap[param1.accessibilityProperties.description];
                if (!_loc_2)
                {
                    return;
                }
                if (_loc_2.areaItem is IToolTipItem)
                {
                    _loc_3 = (_loc_2.areaItem as IToolTipItem).toolTipData;
                }
                else
                {
                    _loc_3 = _loc_2.toolTipData;
                }
                if (_loc_3 != null)
                {
                    this.toolTipContent = ToolTipPool.getObject(_loc_2.toolTipClass as Class);
                    (this.toolTipContent as DisplayObject).visible = true;
                    this.toolTipContent.data = _loc_3;
                    _loc_4 = this.toolTipContent as DisplayObjectContainer;
                    _loc_4.mouseChildren = false;
                    _loc_4.mouseEnabled = false;
                    addChild(_loc_4);
                }
            }
            return;
        }// end function

        public function hide() : void
        {
            this.clearDelayCount();
            this.clearConetnt();
            if (this.area)
            {
                this.area.removeEventListener(MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
                this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.targetMouseHandler);
                this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
                this.area.removeEventListener(Event.REMOVED_FROM_STAGE, this.removedHandler);
                this.area = null;
            }
            visible = false;
            return;
        }// end function

        private function clearConetnt() : void
        {
            var _loc_1:DisplayObject = null;
            if (this.toolTipContent)
            {
                ToolTipPool.disposeObject(this.toolTipContent);
                _loc_1 = this.toolTipContent as DisplayObject;
                _loc_1.visible = false;
                if (_loc_1.hasOwnProperty("dispose"))
                {
                    var _loc_2:* = _loc_1;
                    _loc_2._loc_1["dispose"]();
                }
                if (this.contains(_loc_1))
                {
                    this.removeChild(_loc_1);
                }
                this.toolTipContent = null;
            }
            return;
        }// end function

        public function move(param1:Point) : void
        {
            var _loc_2:* = this.parent.globalToLocal(param1);
            var _loc_3:Boolean = true;
            this.x = _loc_2.x + 20;
            if (this.x > this.toolTipLayer.stage.stageWidth - this.width - 6)
            {
                this.x = _loc_2.x - this.width - 8;
                _loc_3 = false;
            }
            var _loc_4:int = 1;
            this.y = _loc_2.y + 20;
            var _loc_5:* = this.toolTipContent as DisplayObject;
            if (_loc_5)
            {
                if (_loc_5.height > this.toolTipLayer.stage.stageHeight)
                {
                    this.y = 0;
                }
                else if (this.y + _loc_5.height > this.toolTipLayer.stage.stageHeight)
                {
                    this.y = this.toolTipLayer.stage.stageHeight - _loc_5.height;
                }
            }
            if (!visible)
            {
                visible = true;
            }
            if (_loc_5 is ItoolTipEntrance)
            {
                (_loc_5 as ItoolTipEntrance).positionChanged();
                (_loc_5 as ItoolTipEntrance).leftShowMode = _loc_3;
            }
            return;
        }// end function

        private function removedHandler(event:Event) : void
        {
            _instance.hide();
            return;
        }// end function

        private function startDelayCount(param1:int) : void
        {
            if (!this.delayTimer)
            {
                this.delayTimer = new Timer(param1, 1);
                this.delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.showAfterDelay);
            }
            this.delayTimer.reset();
            this.delayTimer.delay = param1;
            this.delayTimer.start();
            this.area.addEventListener(MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
            this.area.addEventListener(MouseEvent.MOUSE_OUT, this.targetMouseHandler);
            this.area.addEventListener(MouseEvent.MOUSE_UP, this.targetMouseHandler);
            return;
        }// end function

        private function clearDelayCount() : void
        {
            if (this.delayTimer)
            {
                this.delayTimer.stop();
            }
            return;
        }// end function

        private function showAfterDelay(event:TimerEvent) : void
        {
            var _loc_2:Point = null;
            if (this.area)
            {
                this.show(this.area);
                _loc_2 = new Point(this.area.mouseX, this.area.mouseY);
                _loc_2 = this.area.localToGlobal(_loc_2);
                this.move(_loc_2);
            }
            return;
        }// end function

        override public function get width() : Number
        {
            if (this.toolTipContent && this.toolTipContent is DisplayObject)
            {
                return (this.toolTipContent as DisplayObject).width;
            }
            return super.width;
        }// end function

        public static function get defaultRenderClass() : Class
        {
            return _defaultRenderClass;
        }// end function

        public static function set defaultRenderClass(param1:Class) : void
        {
            _defaultRenderClass = param1;
            return;
        }// end function

        public static function init(param1:DisplayObjectContainer) : void
        {
            if (_instance == null)
            {
                _instance = new ToolTipsManager(param1);
            }
            return;
        }// end function

        public static function register(param1, param2:Class = null, param3:Object = null) : void
        {
            var _loc_4:String = null;
            if (_instance == null)
            {
                throw new Error("ToolTipsManager 未初始化");
            }
            var _loc_5:* = param1["accessibilityProperties"];
            if (_loc_5 == null)
            {
                var _loc_7:* = new AccessibilityProperties();
                param1["accessibilityProperties"] = new AccessibilityProperties();
                _loc_5 = _loc_7;
                var _loc_8:* = classId + 1;
                classId = _loc_8;
                _loc_4 = "Item" + classId;
                _loc_5.description = _loc_4;
            }
            else
            {
                _loc_4 = _loc_5.description;
            }
            var _loc_6:* = param1 as EventDispatcher;
            _loc_6.addEventListener(MouseEvent.MOUSE_OVER, _instance.targetMouseHandler);
            if (param2 != null)
            {
                toolClassMap[_loc_4] = {toolTipClass:param2, areaItem:param1, toolTipData:param3};
            }
            else if (_defaultRenderClass != null)
            {
                toolClassMap[_loc_4] = {toolTipClass:_defaultRenderClass, areaItem:param1, toolTipData:param3};
            }
            else
            {
                throw new Error("ToolTipManger的dfaultRenderClass未定义");
            }
            return;
        }// end function

        public static function unregister(param1:EventDispatcher) : void
        {
            var _loc_2:String = null;
            var _loc_3:AccessibilityProperties = null;
            if (_instance != null && param1)
            {
                _loc_3 = param1["accessibilityProperties"];
                if (_loc_3)
                {
                    _loc_2 = _loc_3.description;
                }
                toolClassMap[_loc_2] = null;
                delete toolClassMap[_loc_2];
                param1.removeEventListener(MouseEvent.MOUSE_OVER, _instance.targetMouseHandler);
            }
            return;
        }// end function

    }
}
